#pragma once

#include "CanLib/canlib.h"
#include "CANcase/vxlapi.h"
#include "CANcase/xlCANFunctions.h"

#define  SID   0x98da33f1
#define  RID   0x98daf133

/************************************************************************/
/*    UDS protocol define                                               */
/************************************************************************/
#define SVID_RDFI 0x19
#define SVID_CLFI 0x14
#define SVID_NumOfDTCbyMask 0x01
#define SVID_DTCbyMask 0x02
#define SVID_SupportedDTC 0x0A
#define SVID_Ex_Data_DTCbyNum 0x06
#define SVID_Snapshot_DTCbyNum 0x04
#define SVID_RdByID 0x22
#define SVID_WrByID 0x2E
#define SVID_SessionCtrl 0x10
#define SVID_D_Session_Y 0x01
#define SVID_P_Session_Y 0x02
#define SVID_E_Session_Y 0x03
#define SVID_D_Session_N 0x81
#define SVID_P_Session_N 0x82
#define SVID_E_Session_N 0x83
#define SVID_SecurityAccess 0x27
#define SVID_ECUReset 0x11
#define SVID_TesterPresent 0x3E
#define SVID_CommuCtrl 0x28
#define SVID_DTCSettingCtrl 0x85
#define SVID_IOCtrl 0x2F
#define SVID_RoutineCtrl 0x31
#define SVID_LanchRoutine 0x01
#define SVID_GetRoutineResult 0x03
#define SVID_ReqDownload  0x34
#define SVID_TransferData 0x36
#define SVID_ReqTransferExt  0x37


#define NRC_ServiceNotSupp 0x11
#define NRC_SubFunNotSupp 0x12
#define NRC_Message_Format_Error 0x13
#define NRC_BusyRepeatReq 0x21
#define NRC_ConNotMet 0x22
#define NRC_ReqOutOfRange 0x31
#define NRC_InvalidKey 0x35
#define NRC_ExceedMaxAttNo 0x36
#define NRC_TimeDelayNotExpir 0x37
#define NRC_ResponsePending 0x78
#define NRC_NotSuppInSession 0x7F
#define NRC_SecAcc_Denied 0x33


///////////////////////////////////////////////////////////
typedef struct tagCanBusCalSts{
	BOOL IfCanThreadRunning;
	BOOL IfCMDSend;
	BOOL IfConnect;
	BOOL IfUnlock;
	BOOL IfDAQMod;
	BOOL IfWinThreadRunning;
	BOOL IfDisplayAllDTO;
	BOOL IfDataProcThreadRunning;
	BYTE CMD;
	DWORD CanSndID;
	DWORD CanRcvID;
	BYTE FailCount;
	DWORD CurAddress;
	BYTE  CTR;
}CANSTATUS;


class CANSIGFRAME
{
public:
	CANSIGFRAME(void);
	~CANSIGFRAME(void);
	CANSIGFRAME& operator=(CANSIGFRAME &cansig);
	BYTE data[8];
	UINT dlc;
	UINT flag;
	DWORD time;
	long id;
	void *pData;
};


class CCanProtocol
{
public:
	CCanProtocol(void);
	~CCanProtocol(void);

public:
	CANSTATUS   CANStatus;
	//CANSIGFRAME sSignal, rSignal;
	/************************************************************************/
	/* x                                                                   */
	/************************************************************************/
	XLstatus    xlStatus;
	XLevent     sXlEvent, rXlEvent;
	canHandle   m_canHandle;
	int         m_canBitRate;
	BOOL        m_IfCanBusOn;

public:
	CCANFunctions  m_func;

public:
	////////////canlib function 
	bool canStart(void);
	bool canStop(void);

	//BYTE CMD_SEND(DWORD sID, const CANSIGFRAME& sSignal);
	//BYTE CMD_RECV(DWORD rID, CANSIGFRAME& rSignal);
	BYTE CMD_SEND(DWORD sID, XLevent& sXlEvent, int channel = 0);
	BYTE CMD_RECV(DWORD rID, XLevent& rXlEvent);

	BYTE CMD_SEND_ARRAY(DWORD sID, DWORD rID, BYTE *array, WORD length, int channel = 0);

/************************************************************************/
/*  Unified  Diagnostic Services Protocol                               */
/************************************************************************/

public:
	bool UDS_ECUReset(DWORD sID, DWORD rID, BYTE resetType);
	bool UDS_DiagnosticSessionControl(DWORD sID, DWORD rID, BYTE diagnosticSessionType);
	bool UDS_ControlDTCSetting(DWORD sID, DWORD rID, BYTE DTCSettingType);
	bool UDS_CommunicationControl(DWORD sID, DWORD rID, BYTE controlType,BYTE communicationType);
	bool UDS_SecurityAccess(DWORD sID, DWORD rID, BYTE securityAccessType, BYTE securityAccessDataRecord[]);
	bool UDS_RequestDownload(DWORD sID, DWORD rID, BYTE dataFormatIdentifier, BYTE addressAndLengthFormatIdentifier, BYTE* memoryAddress, BYTE* memorySize);
	bool UDS_TransferData(DWORD sID, DWORD rID, WORD blockSequenceCounter, BYTE* transferRequestParameterRecord,WORD transferLength);
	bool UDS_RequestTransferExit(DWORD sID, DWORD rID, BYTE transferRequestParameterRecord);
};
